package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentTradePublishBinding;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.PublishTradeDetailEntity;
import com.sgb.kjwl.utils.DisplayUtil;
import com.sgb.kjwl.utils.GlideEngine;
import com.sgb.kjwl.utils.KeyboardUtils;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.utils.TextSizeUtils;
import com.sgb.kjwl.utils.UploadCheckHelper;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverPublishActivity;
import com.sgb.kjwl.viewmodel.enterprise.PublishViewModel;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TradePublishFragment extends AppFragment {


    FragmentTradePublishBinding mBinding;
    PublishViewModel mViewModel;
    private String category;
    private String categoryName;
    private String mId;
    OptionsPickerView<IpPickSelectEntity> mOptionsPickerView;
    private MakeOverPublishActivity mActivity;
    private List<File> list = new ArrayList<>();
    private String mImage = "", mColor = "", mSelectColor = "";
    private boolean isChecked, isCheckAuto, isCheck, isCheckGold, isCheckRed, isCheckBlue, isCheckGray;
    private PublishTradeDetailEntity entity;

    public static TradePublishFragment newInstance() {
        return new TradePublishFragment();
    }

    public static TradePublishFragment newInstance(PublishTradeDetailEntity entity) {
        TradePublishFragment tradePublishFragment = new TradePublishFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity", entity);
        tradePublishFragment.setArguments(bundle);
        return tradePublishFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_trade_publish, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.setLifecycleOwner(this);
        mViewModel = new ViewModelProvider(this).get(PublishViewModel.class);

        initCheckBox();
        initEditText();
        initButton();
        initData();
    }

    private void initCheckBox() {

        mBinding.ivTag2.setImageResource(R.mipmap.weigouxuan_makeover_publish);
        mBinding.ivTag1.setImageResource(R.mipmap.yigouxuan_makeover_publish);
        mBinding.tvFragmentTradePublishUploadPictureTags.setVisibility(View.VISIBLE);
        mBinding.llFragmentTradePublishPictureTags.setVisibility(View.GONE);
        isCheckAuto = false;
        isCheck = true;
        mBinding.deleteTv.setVisibility(View.GONE);

        mBinding.ivTag3.setImageResource(R.mipmap.yigouxuan_makeover_publish);
        mBinding.ivTag6.setImageResource(R.mipmap.weigouxuan_makeover_publish);
        mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
        mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
        isCheckRed = false;
        isCheckBlue = false;
        isCheckGray = false;
        isCheckGold = true;
        mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xffB79255);

        mBinding.rbFragmentTradePublishMakeoverSelectorPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isChecked = !isChecked;
                if (isChecked) {
                    mBinding.ivYijia.setImageResource(R.mipmap.gouxuan_makeover_publish_price);
                    mBinding.etFragmentTradePublishMakeoverMuchEdit.setEnabled(false);
                    mBinding.etFragmentTradePublishMakeoverMuchEdit.setText("请输入价格");
                    mBinding.etFragmentTradePublishMakeoverMuchEdit.setTextColor(getResources().getColor(R.color.color_C0C4CC));
                } else {
                    mBinding.ivYijia.setImageResource(R.mipmap.weigouxuan_makeover_publish_price);
                    mBinding.etFragmentTradePublishMakeoverMuchEdit.setEnabled(true);
                    mBinding.etFragmentTradePublishMakeoverMuchEdit.setText("");
                    mBinding.etFragmentTradePublishMakeoverMuchEdit.setTextColor(getResources().getColor(R.color.FF000000));
                }
            }
        });

        mBinding.ivTag1.setImageResource(R.mipmap.yigouxuan_makeover_publish);
        isCheck = true;
        mBinding.tvFragmentTradePublishUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCheck = !isCheck;
                if (isCheck) {
                    mImage = "";
                    mColor = "";
                    mBinding.ivTag2.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag1.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                    mBinding.tvFragmentTradePublishUploadPictureTags.setVisibility(View.VISIBLE);
                    mBinding.llFragmentTradePublishPictureTags.setVisibility(View.GONE);
                    isCheckAuto = false;
                }
//                else {
//                    mBinding.ivTag1.setImageResource(R.mipmap.weigouxuan_makeover_publish);
//                }
            }
        });

        mBinding.tvFragmentTradePublishAutoUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCheckAuto = !isCheckAuto;
                if (isCheckAuto) {
                    mBinding.ivTag1.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag2.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                    mBinding.tvFragmentTradePublishUploadPictureTags.setVisibility(View.GONE);
                    mBinding.llFragmentTradePublishPictureTags.setVisibility(View.VISIBLE);
                    isCheck = false;

                    mColor = mSelectColor;
                    if (mColor.equals("") && mSelectColor.equals("")) {
                        mColor = "#B79255";
                        mBinding.ivTag3.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                        mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xffB79255);
                    }
                }
//                else {
//                    mBinding.ivTag2.setImageResource(R.mipmap.weigouxuan_makeover_publish);
//                }
            }
        });

        mBinding.tvFragmentTradePublishGold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCheckGold = !isCheckGold;
                if (isCheckGold) {
                    mColor = "#B79255";
                    mSelectColor = "#B79255";
                    mBinding.ivTag3.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                    mBinding.ivTag6.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    isCheckRed = false;
                    isCheckBlue = false;
                    isCheckGray = false;
                    mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xffB79255);
                } else {
                    mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                }
            }
        });

        mBinding.tvFragmentTradePublishRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCheckRed = !isCheckRed;
                if (isCheckRed) {
                    mColor = "#E1251B";
                    mSelectColor = "#E1251B";
                    mBinding.ivTag4.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                    mBinding.ivTag6.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    isCheckGold = false;
                    isCheckBlue = false;
                    isCheckGray = false;
                    mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xffE1251B);
                } else {
                    mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                }
            }
        });

        mBinding.tvFragmentTradePublishBlue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCheckBlue = !isCheckBlue;
                if (isCheckBlue) {
                    mColor = "#0286DF";
                    mSelectColor = "#0286DF";
                    mBinding.ivTag5.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                    mBinding.ivTag6.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    isCheckGold = false;
                    isCheckRed = false;
                    isCheckGray = false;
                    mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xff0286DF);
                } else {
                    mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                }
            }
        });

        mBinding.tvFragmentTradePublishGray.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCheckGray = !isCheckGray;
                if (isCheckGray) {
                    mColor = "#c0c4cc";
                    mSelectColor = "#c0c4cc";
                    mBinding.ivTag6.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                    mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                    isCheckGold = false;
                    isCheckRed = false;
                    isCheckBlue = false;
                    mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xffc0c4cc);
                } else {
                    mBinding.ivTag6.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                }
            }
        });


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity.mLiveData.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {



                    if (!UploadCheckHelper.textCheck(getContext(), new TextView[]{mBinding.tvFragmentTradePublishSelectType, mBinding.etFragmentTradePublishTradeRegisterEdit, mBinding.etFragmentTradePublishTradeName, mBinding.etFragmentTradePublishMakeoverMuchEdit
                            , mBinding.tvFragmentTradePublishLinkman, mBinding.tvFragmentTradePublishPhone}, new String[]{"商标分类", "商标注册号", "商标名称", "价格", "联系人姓名", "联系电话"})) {
                        return;
                    }

                    if( mBinding.etFragmentTradePublishMakeoverMuchEdit.isEnabled()&&Double.parseDouble(mBinding.etFragmentTradePublishMakeoverMuchEdit.getText().toString())==0){
                        MToast.showToast(getContext(), "交易价格大于0");
                        return;
                    }

                    if (!CheckUtils.isMobileForService(mBinding.tvFragmentTradePublishPhone.getText().toString().trim())) {
                        MToast.showToast(getContext(), "请输入正确的电话格式");
                        return;
                    }
                    if (isCheckAuto) {
                        list.add(shotGroupView(mBinding.tvFragmentTradePublishUploadPicture));
                    }

                    if (mImage.equals("") && (list == null || list.size() <= 0)) {
                        MToast.showToast(getContext(), "请上传商标图片");
                        return;
                    }

                    if (mImage.equals("")) {
                        mViewModel.uploadPicture(getContext(), list);
                    } else {
                        upload();
                    }
                }
            }
        });
    }

    private void initEditText() {
        mBinding.etFragmentTradePublishTradeName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mBinding.etFragmentTradePublishTradeName.getText().toString().length() == 1) {
                    mBinding.tvFragmentTradePublishUploadPicture.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
                } else {
                    mBinding.tvFragmentTradePublishUploadPicture.setTextSize(TypedValue.COMPLEX_UNIT_SP, (Math.max((20 - 2 * (mBinding.etFragmentTradePublishTradeName.getText().toString().length() / 2)), 10)));
                }
                mBinding.tvFragmentTradePublishUploadPicture.setText(TextSizeUtils.singleTextLine((DisplayUtil.dip2px(getContext(), 120.0f) - DisplayUtil.dip2px(getContext(), 5.0f) * 2), mBinding.tvFragmentTradePublishUploadPicture.getTextSize(), mBinding.etFragmentTradePublishTradeName.getText().toString()));
            }
        });
    }

    private void initButton() {
        mBinding.tvFragmentTradePublishSelectType.setOnClickListener(this);
        mBinding.ivFragmentTradePublishUploadPicture.setOnClickListener(this);
        mBinding.deleteTv.setOnClickListener(this);
    }

    private void initData() {
        if (getArguments() != null) {
            entity = (PublishTradeDetailEntity) getArguments().getSerializable("entity");

        }

        if (entity != null) {
            mId = entity.getId() + "";
            mImage = entity.getImg();

            mBinding.tvFragmentTradePublishSelectType.setText(entity.getCategoryName());
            mBinding.etFragmentTradePublishTradeRegisterEdit.setText(entity.getRegistrationNo());
            mBinding.etFragmentTradePublishTradeName.setText(entity.getName());
            mBinding.tvFragmentTradePublishLinkman.setText(entity.getContacts());
            mBinding.tvFragmentTradePublishPhone.setText(entity.getContactNumber());
            mBinding.tvFragmentTradePublishMakeCircle.setText(entity.getRange());
            mBinding.tvFragmentTradePublishIntroduction.setText(entity.getExplain());
            mBinding.tvFragmentTradePublishUploadPicture.setText(entity.getName());

            if (entity.getPrice() == -1) {
                isChecked = true;
                mBinding.ivYijia.setImageResource(R.mipmap.gouxuan_makeover_publish_price);
                mBinding.etFragmentTradePublishMakeoverMuchEdit.setEnabled(false);
                mBinding.etFragmentTradePublishMakeoverMuchEdit.setText("请输入价格");
                mBinding.etFragmentTradePublishMakeoverMuchEdit.setTextColor(getResources().getColor(R.color.color_C0C4CC));
            } else {
                mBinding.ivYijia.setImageResource(R.mipmap.weigouxuan_makeover_publish_price);
                mBinding.etFragmentTradePublishMakeoverMuchEdit.setEnabled(true);
                mBinding.etFragmentTradePublishMakeoverMuchEdit.setText(PointUtils.originalPrice(entity.getPrice()));
                mBinding.etFragmentTradePublishMakeoverMuchEdit.setTextColor(getResources().getColor(R.color.FF000000));
            }

            if (entity.getColor() != null && !entity.getColor().equals("")) {
                mBinding.ivTag1.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                mBinding.ivTag2.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                mBinding.tvFragmentTradePublishUploadPictureTags.setVisibility(View.GONE);
                mBinding.llFragmentTradePublishPictureTags.setVisibility(View.VISIBLE);
                isCheck = false;
                isCheckAuto = true;
                mColor = entity.getColor();

                switch (mColor) {
                    case "#B79255":
                        mColor = "#B79255";
                        mSelectColor = "#B79255";
                        mBinding.ivTag3.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                        mBinding.ivTag6.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        isCheckGray = false;
                        isCheckBlue = false;
                        isCheckRed = false;
                        isCheckGold = true;
                        mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xffB79255);
                        break;
                    case "#E1251B":
                        mColor = "#E1251B";
                        mSelectColor = "#E1251B";
                        mBinding.ivTag4.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                        mBinding.ivTag6.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        isCheckGold = false;
                        isCheckBlue = false;
                        isCheckGray = false;
                        isCheckRed = true;
                        mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xffE1251B);
                        break;
                    case "#0286DF":
                        mColor = "#0286DF";
                        mSelectColor = "#0286DF";
                        mBinding.ivTag5.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                        mBinding.ivTag6.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        isCheckGray = false;
                        isCheckRed = false;
                        isCheckGray = false;
                        isCheckBlue = true;
                        mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xff0286DF);
                        break;
                    default:
                        mColor = "#c0c4cc";
                        mSelectColor = "#c0c4cc";
                        mBinding.ivTag6.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                        mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                        isCheckRed = false;
                        isCheckBlue = false;
                        isCheckGold = false;
                        isCheckGray = true;
                        mBinding.tvFragmentTradePublishUploadPicture.setTextColor(0xffc0c4cc);
                        break;
                }
            } else {
                mBinding.ivTag2.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                mBinding.ivTag1.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                mBinding.tvFragmentTradePublishUploadPictureTags.setVisibility(View.VISIBLE);
                mBinding.llFragmentTradePublishPictureTags.setVisibility(View.GONE);
                isCheckAuto = false;
                isCheck = true;
                mColor = entity.getColor();

                Glide.with(getContext()).load(entity.getImg()).apply(new RequestOptions()
                        .placeholder(R.mipmap.makeover_add_picture)
                        .fallback(R.mipmap.makeover_add_picture)
                        .error(R.mipmap.makeover_add_picture)).into(mBinding.ivFragmentTradePublishUploadPicture);
            }

        }

        mViewModel.getTrademarkType(getContext());
        mViewModel.mTrademarkTypeListLiveData.observe(getViewLifecycleOwner(), new Observer<List<IpPickSelectEntity>>() {
            @Override
            public void onChanged(List<IpPickSelectEntity> ipPickSelectEntities) {
                if (mOptionsPickerView == null) {
                    mOptionsPickerView = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int options2, int options3, View v) {
                            mBinding.tvFragmentTradePublishSelectType.setText(ipPickSelectEntities.get(options1).getName());
                            category = ipPickSelectEntities.get(options1).getValue();
                            categoryName = ipPickSelectEntities.get(options1).getName();
                        }
                    }).setCancelText("取消").setSubCalSize(15)
                            .setCancelColor(Color.parseColor("#666666"))
                            .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                            .setSubmitText("确定")
                            .setSubmitColor(Color.parseColor("#5792FD"))
                            .setTextColorCenter(Color.parseColor("#ff333333"))
                            .setTitleText("选择商标分类")
                            .setContentTextSize(15)
                            .setLineSpacingMultiplier(2.5f)
                            .setDividerColor(Color.parseColor("#EDEDED"))
                            .setDecorView(getActivity().getWindow().getDecorView().findViewById(android.R.id.content)).build();
                    mOptionsPickerView.setPicker(ipPickSelectEntities);
                    mOptionsPickerView.setSelectOptions(0);
                }
                KeyboardUtils.hideSoftInput(getActivity());
            }
        });

        mViewModel.mSaveSuccessTrademark.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
//                    getActivity().setResult(0);

                    EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_CREATE_NEWMAKEOVERDATA).setObject(0));
                    getActivity().finish();
//                    ActivityJump.jumpToActivity(getContext(), MakeOverPublishMainActivity.class, Pair.create("position", 0));
                }
            }
        });
        mActivity = (MakeOverPublishActivity) getActivity();

        mViewModel.uploadFileSuccess.observe(getViewLifecycleOwner(), new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> list) {
                mImage = list.get(0);
                upload();
            }
        });
    }

    private void initFileSelector() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .selectionMode(PictureConfig.SINGLE)
                .loadImageEngine(GlideEngine.createGlideEngine())
                .compress(true)//是否压缩
                .minimumCompressSize(4 * 1024)//小于4M不压缩
                .maxSelectNum(1)//判断还能选多少张图片
                .forResult(PictureConfig.CHOOSE_REQUEST);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_fragment_trade_publish_select_type:
                if (mOptionsPickerView != null) {
                    mOptionsPickerView.show();
                } else {
                    mViewModel.getTrademarkType(getContext());
                }
                break;
            case R.id.iv_fragment_trade_publish_upload_picture:
                initFileSelector();
                break;
            case R.id.delete_tv:
                mBinding.ivFragmentTradePublishUploadPicture.setImageResource(R.mipmap.makeover_add_picture);
                list.clear();
                break;
        }
    }

    public void upload() {
//        if (!CheckUtils.isMobile(mBinding.tvFragmentTradePublishPhone.getText().toString())){
//            MToast.showToast(getContext(),"请输入正确的电话格式");
//            return;
//        }

        String companyNo = "0";
        String teamNo = "0";
        if (SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0) == 1) {
            companyNo = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "");
        } else if (SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0) == 2) {
            teamNo = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "");
        } else {

        }

        Map<String, Object> map = new HashMap<>();
        map.put("category", category);
        map.put("categoryName", categoryName);
        map.put("color", mColor);
        map.put("companyNo", companyNo);
        map.put("contactNumber", mBinding.tvFragmentTradePublishPhone.getText().toString());
        map.put("contacts", mBinding.tvFragmentTradePublishLinkman.getText().toString());
        map.put("explain", mBinding.tvFragmentTradePublishIntroduction.getText().toString());
        map.put("id", mId);
        map.put("img", mImage);
        map.put("name", mBinding.etFragmentTradePublishTradeName.getText().toString());
        map.put("price", mBinding.etFragmentTradePublishMakeoverMuchEdit.getText().toString().equals("请输入价格") ? "-1" : mBinding.etFragmentTradePublishMakeoverMuchEdit.getText().toString());
        map.put("publishType", SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0));
        map.put("range", mBinding.tvFragmentTradePublishMakeCircle.getText().toString());
        if (SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0) == 0) {
            map.put("realName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME, ""));
        } else {
            map.put("realName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NAME, ""));
        }
        map.put("registrationNo", mBinding.etFragmentTradePublishTradeRegisterEdit.getText().toString());
        map.put("teamNo", teamNo);
        map.put("telPhone", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LOGIN_PHONE, ""));
        map.put("type", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LOGIN_PHONE, ""));
        mViewModel.saveAndUpdateTrademark(getContext(), map);
    }

    /**
     * 获取ViewGroup屏幕截图
     */
    public File shotGroupView(TextView group) {
        int h = 0;
        Bitmap bitmap;
        h += group.getHeight();
        group.setBackgroundColor(Color.parseColor("#f5f5f5"));
        bitmap = Bitmap.createBitmap(group.getWidth(), h, Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        group.draw(canvas);
        mImage = "";
        return mActivity.saveBitmapFile(bitmap, "/" + System.currentTimeMillis() + ".jpg");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureConfig.CHOOSE_REQUEST) {
            list.clear();
            List<LocalMedia> imageList = PictureSelector.obtainMultipleResult(data);
            if (imageList == null || imageList.size() == 0) return;
            mImage = "";
            //获取压缩图片
            for (LocalMedia localMedia : imageList) {
                list.add(new File(localMedia.getCompressPath()));
            }

            Glide.with(getContext()).load(list.get(0)).into(mBinding.ivFragmentTradePublishUploadPicture);
            mBinding.deleteTv.setVisibility(View.VISIBLE);
        }

    }
}
